<?php
//店铺设置
namespace Store\Controller;
//use Think\Controller;
use Store\Controller\StoreController;
class CategoryController extends StoreController
{
    function _initialize() {
        parent::_initialize();
        $this->assign('sys', 'goods'); // 数据集
    }
    
    //商品分类管理
    public function index(){
        $session = session();
        $good_class = M('Store_goods_class')->where(array('store_id'=>$session['store_id']))->select();
        $tree = new \Libs\Tree\Tree();
        $arr = $tree->channelLevel($good_class, '0', '&nbsp;', 'stc_id', 'stc_parent_id');
        $this->assign('arr',$arr);
        $this->assign('action', 'category');
        $this->display();
    }

    //添加分类
    public function add(){
        if(IS_POST){
            $datas = I('post.');
            $datas['store_id'] = session()['store_id'];
            $tf = M('Store_goods_class')->data($datas)->add();
            if($tf){
                $this->success('新增成功', U('index'));
            }else{
                $this->error('新增失败');
            }
        }
        $this->assign('action', 'category');
        $this->display();
    }

    //修改分类
    public function save(){
        $where['stc_id'] = I('get.stc_id');
        $where['store_id'] = session('store_id');
        $good_class = M('Store_goods_class');
        $arr = $good_class->where($where)->find();
        $this->assign('arr',$arr);
        if(IS_POST){
            $datas = I('post.');
            $tf = $good_class->where($where)->data($datas)->save();
            if($tf !== FALSE){
                $this->success('操作成功', U('index'));
            }else{
                $this->error('操作失败');
            }
        }
        $this->assign('action', 'category');
        $this->display();
    }

    //删除分类
    public function del(){
        $Id = I('post.data', 0, 'intval');
        if (empty($Id)) {
            $this->error('参数不能为空！');
        }
        $where['stc_id'] = $Id;
        $where['store_id'] = session()['store_id'];
        $good_class = M('Store_goods_class')->where($where)->delete();
        if($good_class){
            $this->success('操作成功', U('index'));
        }else{
            $this->error('操作失败');
        }
    }
}